package com.hushan.standby.compoment;

import cn.hutool.core.net.NetUtil;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

import static com.hushan.standby.App.VIRTUAL_GATEWAY;

public class LeaderElection {
    public static AtomicBoolean master = new AtomicBoolean(false);
    public static AtomicLong checkPoint = new AtomicLong(0);

    public static synchronized void startSlave() {
        master.set(false);
        VirtualIPControl.startSlave();
        //RedisReplicaControl.startSlave();
        //MySQLReplicaControl.startSlave();
    }

    public static synchronized void startMonitor() {
        ArpRequester.startMonitor();
        //SlaveNotifier.startNotify();
        //RedisReplicaControl.startNotify();
        //MySQLReplicaControl.startNotify();

    }

    public static synchronized void startMaster() {
        if (NetUtil.ping(VIRTUAL_GATEWAY, 300)) {
            master.set(true);
            checkPoint.set(System.currentTimeMillis());
            VirtualIPControl.stopSlave();
            //RedisReplicaControl.stopSlave();
            //MySQLReplicaControl.stopSlave();
        } else {
            startSlave();
        }
    }
}
